package ai.people.netmon.framework.domain.warning.vo;

import ai.people.netmon.framework.domain.warning.entity.WarningAccount;
import ai.people.netmon.framework.domain.warning.entity.WarningAccountLabelRelation;
import ai.people.netmon.utils.StringPool;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.annotation.JSONField;
import com.alibaba.fastjson.serializer.ToStringSerializer;
import lombok.Data;

import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 加入分析账户
 *
 * @author yuanqinglong
 * @date 2022/8/4 14:03
 */
@Data
public class JoinAnalysisAccountVO {


    /**
     * id
     */
    @JSONField(serializeUsing = ToStringSerializer.class)
    private Long id;

    /**
     * 账户
     * @mock xxx
     */
    private String account;

    /**
     * 账户平台
     * @mock twitter
     */
    private String platform;
    /**
     * 平台名称
     */
    private String platformName;

    /**
     * 标签
     * @mock 反政府
     */
    private String accountLabel;
    /**
     * 帐户标签名称
     */
    private String accountLabelName;

    /**
     * 账户昵称
     * @mock go
     */
    private String accountNickName;

    /**
     * 账户头像
     */
    private String accountAvatar;

    /**
     * 账户简介
     */
    private String accountSummary;


    /**
     * 构建
     *
     * @param warningAccount 警告帐户
     * @param platformMap    平台地图
     * @param accountLabels  帐户标签
     * @return {@link JoinAnalysisAccountVO}
     */
    public static JoinAnalysisAccountVO build(WarningAccount warningAccount, Map<String, String> platformMap, Map<Long, List<WarningAccountLabelRelation>> accountLabels){
        JoinAnalysisAccountVO joinAnalysisAccountVO = JSON.parseObject(JSON.toJSONString(warningAccount), JoinAnalysisAccountVO.class);
        joinAnalysisAccountVO.setPlatformName(platformMap.getOrDefault(joinAnalysisAccountVO.getPlatform(), StringPool.EMPTY));
        List<WarningAccountLabelRelation> labelRelationList = accountLabels.getOrDefault(warningAccount.getId(), Collections.emptyList());
        String labelNames = labelRelationList.stream().map(WarningAccountLabelRelation::getLabelName).collect(Collectors.joining(StringPool.COMMA));
        String labelValues = labelRelationList.stream().map(WarningAccountLabelRelation::getLabelValue).collect(Collectors.joining(StringPool.COMMA));
        joinAnalysisAccountVO.setAccountLabel(labelValues);
        joinAnalysisAccountVO.setAccountLabelName(labelNames);
        return joinAnalysisAccountVO;
    }

}
